import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import path from "path";

import routes from "./router/index.js";
import errorHandler from "./middleware/error.middleware.js";
import notfoundHandler from "./middleware/notfound.middleware.js";
import logHandler from "./middleware/log.middleware.js";
import logger from './utils/logger.js'

BigInt.prototype.toJSON = function () {
  return Number(this.toString());
};

dotenv.config();
const app = express();
app.use(
  cors({
    origin: process.env.FRONTEND_URL,
    credentials: true,
  }),
);
app.use(logHandler);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/uploads", express.static(path.resolve("uploads")));

app.get("/", (req, res) => res.send("Cloud Drive API is running..."));
app.use("/api", routes);
app.use(notfoundHandler);
app.use(errorHandler);

const PORT = Number(process.env.PORT) || 4000;
app.listen(PORT, () =>
  logger.info(`Server running ot PROT:${PORT}`),
);
